package com.cqut.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cqut.model.CodeManager;
import com.cqut.dao.CodeManagerMapper;
import com.cqut.dao.CodeManagerMapperExtra;
import com.cqut.dto.CodeManagerDTO;
import com.cqut.dto.LimitShowDTO;
import com.cqut.service.ICodeManagerService;
import com.cqut.util.BeanUtil;
import com.cqut.util.SqlUtil;
import com.cqut.util.StringUtil;
import com.cqut.util.SystemConstants;

@Service
public class CodeManagerService implements ICodeManagerService {
    @Resource(name="codeManagerMapper")
    private CodeManagerMapper codeManagerMapper;
    
    @Resource(name="codeManagerMapperExtra")
    private CodeManagerMapperExtra codeManagerMapperExtra;

    @Override
    public boolean save(CodeManager codeManager) {
        return codeManagerMapper.insert(codeManager) > 0;
    }

    @Override
    public boolean update(CodeManager codeManager) {
        return codeManagerMapper.updateByPrimaryKeySelective(codeManager) >= 0;
    }

    @Override
    public boolean delete(String codeId) {
        return codeManagerMapper.deleteByPrimaryKey(codeId) > 0;
    }

    @Override
    public boolean batchDelete(String[] primaryKeys) {
        return codeManagerMapper.deleteByPrimaryKeys(primaryKeys) > 0;
    }

    @Override
    public CodeManager findModel(String codeId) {
        return codeManagerMapper.selectByPrimaryKey(codeId);
    }

    @Override
    public CodeManager findModel(String codeId, String[] parameters) {
        return codeManagerMapper.findModel(codeId, SqlUtil.formatParameters(parameters));
    }

    public Map<String, Object> findMap(String codeId) {
        String[][] parameters = {{"code_id", "codeId"}, {"code_no", "codeNo"}, {"code_type", "codeType"}, {"code_name", "codeName"}, {"code_level", "codeLevel"}};
        return this.findMap(codeId, parameters);
    }

    public Map<String, Object> findMap(String codeId, String[][] parameters) {
        return codeManagerMapper.findMap(codeId, SqlUtil.formatParametersForAlias(parameters));
    }

    @Override
    public List<CodeManager> loadModels() {
        return this.loadModels(null, null, null, null, -1, -1);
    }

    @Override
    public List<CodeManager> loadModels(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return this.loadModels(null, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<CodeManager> loadModels(String[] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return codeManagerMapper.loadModels(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps() {
        String[][] parameters = {{"code_id", "codeId"}, {"code_no", "codeNo"}, {"code_type", "codeType"}, {"code_name", "codeName"}, {"code_level", "codeLevel"}};
        return this.loadMaps(parameters, null, null, null, null, -1, -1);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        String[][] parameters = {{"code_id", "codeId"}, {"code_no", "codeNo"}, {"code_type", "codeType"}, {"code_name", "codeName"}, {"code_level", "codeLevel"}};
        return this.loadMaps(parameters, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String[][] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return codeManagerMapper.loadMaps(SqlUtil.formatParametersForAlias(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public long count() {
        return this.count(null, false);
    }

    @Override
    public long count(String condition, Object[] values) {
        return this.count(null, condition, values, false);
    }

    @Override
    public long count(String[] parameters, boolean isDistinct) {
        return this.count(parameters, null, null, isDistinct);
    }

    @Override
    public long count(String[] parameters, String condition, Object[] values, boolean isDistinct) {
        return codeManagerMapper.count(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), isDistinct);
    }

    /**
	 * 新增码表
	 * */
	@Override
	@Transactional
	public boolean addTableCode(CodeManagerDTO codeManagerDTO) {
		CodeManager tableCode = codeManagerDTO.toModel();
		String parentCode = tableCode.getParentCode();
		
		if (StringUtil.isNullOrEmpty(parentCode)) {
			parentCode = null;
		}
		
//		if (tableCodeMapperExtra.judgeRepetition(tableCode.getParamName(), tableCode.getCodeType(), null) > 0)
//			return REPETITION_KEY;
		
		tableCode.setCodeId(creatTreeParamCode(parentCode));
		tableCode.setCodeLevel(creatLevel0(parentCode));
		
		if (codeManagerMapper.insertSelective(tableCode) <= 0)
			return false;
		
		return true;
	}
	
	/**
	 * 根据parentCode生成树形paramCodeId
	 * */
	private String creatTreeParamCode(String parentCode) {
		Map<String, Object> maxCode = codeManagerMapperExtra.findMaxCodeId(parentCode);
		
		if (maxCode == null)
			return BeanUtil.getTreeCodeForInit(parentCode);
		      
		return BeanUtil.getTreeCode(maxCode.get("maxCode").toString());
	}
    
	
	/**
	 * 根据parentCode生成level
	 * */
	private int creatLevel0(String parentCode) {
		CodeManager tableCode = codeManagerMapper.selectByPrimaryKey(parentCode);
		
		if (tableCode == null)
			return 1;
		
		return tableCode.getCodeLevel() + 1;
	}
	
    /**
     * 根据搜索码表名进行筛选
     * @param codeName
     * @param limitShowDTO
     * @return
     *//*
    @Override
	public List<Map<String, Object>> findCodes(String codeName,
			LimitShowDTO limitShowDTO) {
		return codeManagerMapperExtra.findCodes(codeName,limitShowDTO.calculateStart(), limitShowDTO.calculateEnd());
	}
    */
    /**
	 * 判断码表名称是否重复
	 * */
	@Override
	public boolean duplicateCheckingCodeName(String codeName) {
		return codeManagerMapperExtra.duplicateCheckingCodeName(codeName) != null;
	}

	/**
	 * 判断码表类型是否重复
	 * */
	public boolean CheckingCodeType(String codeType) {
		return codeManagerMapperExtra.CheckCodeType(codeType)>0;
	}
	
	/**
	 * 码表类型找到当前节点的子节点
	 * */
	@Override
	public List<Map<String, Object>> findChildrenByCodeType(String codeType) {
		
		return codeManagerMapperExtra.getCodeType(codeType);
	}
	
	/**
	 * 删除码表(please use this method)
	 * */
	@Override
	@Transactional
	public boolean deleteTableCodesByParamCodes(String[] codeNoArray) {
		//String parentCode = tableCodeMapper.selectByPrimaryKey(paramCodeArray[0]).getParentCode(); //获取要删除的节点的父节点
		
		if (codeManagerMapperExtra.deleteByLikeParamCode(codeNoArray) <= 0)
			return false;
		return true;
	}
	
	/**
	 * 获得list
	 * @param codeName
	 * **/
	public List<Map<String, Object>> getCodeList(String codeName,String parentId,LimitShowDTO limitShowDTO){
		return codeManagerMapperExtra.getListByCodeName(codeName,parentId,limitShowDTO.calculateStart(),limitShowDTO.calculateEnd());
	}
	
	/**
	 * 获得list的count数
	 * **/
	public long getCodeListCount(String parentId,String codeName){
		return codeManagerMapperExtra.getListCountByCodeName(parentId,codeName);
	}
	
	/**
	 * 根据id获得一条数据
	 * **/
	public Map<String, Object> getDetailById(String codeId){
		return codeManagerMapperExtra.getDetailById(codeId);
	}
	
	
	//修改
	public Map<String,Object> ResponseEntity(CodeManagerDTO codeManagerDTO){
		return codeManagerMapperExtra.updateCode(codeManagerDTO.getCodeId(),codeManagerDTO.getCodeName());
	}

}
